package com.bdqn.vueboot.controller;


import com.bdqn.vueboot.entity.User;
import com.bdqn.vueboot.service.UserService;
import com.bdqn.vueboot.util.JwtTokenUtil;
import com.bdqn.vueboot.util.LoginState;
import io.swagger.annotations.*;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@Api
//注解如果写到类上默认该类的所有方法都会添加该注解
//@CrossOrigin
public class LoginController {

    @Resource
    UserService userService;

    @Resource
    StringRedisTemplate stringRedisTemplate;

    @RequestMapping(value = "/dologin")
    @CrossOrigin
//    public String dologin(@RequestParam(value = "usrName") String usrName,
//                          @RequestParam(value = "usrPassword") String usrPassword,
//                          HttpServletResponse response) {
    public String dologin(@RequestBody User loginUser,
                          HttpServletResponse response) {
        //设置相应头token属性,否则response.setHeader("token", token);无法有效
        response.setHeader("Access-Control-Expose-Headers", "token");
        User user = userService.findByUserNameBiz(loginUser.getUsrName());
        if (user == null) {
            return LoginState.NOTEXIST;
        } else {
            if (!user.getUsrPassword().equals(loginUser.getUsrPassword())) {
                return LoginState.PASSWORDWERROR;
            } else {
                //String uuid = UUID.randomUUID().toString();
                //System.out.println("uuid:" + uuid);
                try {
                    String token = JwtTokenUtil.createSign(user.getUsrName());
                    //在执行完登录操作后,会讲token放到response响应头中讲token返回给客户端
                    response.setHeader("token", token);
                } catch (Exception e) {
                    e.printStackTrace();
                    return LoginState.PASSWORDWERROR;
                }
                //stringRedisTemplate.opsForValue().set(uuid, user.getUsrName(), 600, TimeUnit.SECONDS);
                return LoginState.SUCCESS;
            }
        }
    }

    @RequestMapping(value = "/logout")
    @CrossOrigin
    public Object logout(HttpServletRequest request) {
        String token = request.getHeader("token");
        Boolean bool = stringRedisTemplate.delete(token);
        return bool;
    }


    @ApiOperation(value = "登录操作测试")
    @PostMapping(value = "/dologintest")
    @CrossOrigin
    public String dologintest(
            @ApiParam(name = "loginUser", value = "用户实体", required = true)
            @RequestBody User loginUser,
            HttpServletResponse response) {
        //解决post请求的异常
    /*public String dologin(@RequestBody Map map,
                          HttpServletResponse response) {*/
        response.setHeader("Access-Control-Expose-Headers", "token");
        User user = userService.findByUserNameBiz(loginUser.getUsrName());
        if (user == null) {
            return LoginState.NOTEXIST;
        } else {
            if (!user.getUsrPassword().equals(loginUser.getUsrPassword())) {
                return LoginState.PASSWORDWERROR;
            } else {
                String uuid = UUID.randomUUID().toString();
                System.out.println("uuid:" + uuid);
                response.setHeader("token", uuid);
                stringRedisTemplate.opsForValue().set(uuid, user.getUsrName(), 600);
                return LoginState.SUCCESS;
            }
        }
    }
}
